# -*- coding: utf-8 -*-
from five import grok
from zope import schema
from plone.directives import form
from plone.app.textfield import RichText
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
from z3c.relationfield.schema import RelationList, RelationChoice
from plone.formwidget.contenttree import ObjPathSourceBinder,ContentTreeFieldWidget
from Products.bpContents import _

class IAviso(form.Schema):
    """Un aviso a los usuarios de la biblioteca y al público general
    """

    form.order_before(prioridad='IBasic.description')
    prioridad=schema.Choice(
       title=_(u"Prioridad"),
       vocabulary=SimpleVocabulary(
	[SimpleTerm(value=u'Baja', title=_(u'Baja')), 
	SimpleTerm(value=u'Media', title=_(u'Media')),
	SimpleTerm(value=u'Alta', title=_(u'Alta'))]
    	),
        )
    
    cuerpo = RichText(
        title=_(u"Cuerpo"),
        description=_(u"Texto principal"),
    )

    relaciones = RelationList(
        title=_(u"Documentos Relacionados"),
        description=_(u"Otros articulos o galerias que complementen el documento actual"),
        value_type=RelationChoice(
            source=ObjPathSourceBinder(

            ),
        ),
        required=False,
    )
    
    
from plone.app.discussion.interfaces import IConversation
from zope.component import getMultiAdapter
from plone.memoize.instance import memoize
from Products.CMFCore.utils import getToolByName
from plone.directives.dexterity import DisplayForm
from Products.bpContents.BaseClassView import BaseClassView


class View(DisplayForm,BaseClassView):
    grok.context(IAviso)
    grok.require('zope2.View')


    def getImages(self):
        """busca en el cuerpo del texto si tiene una marca IMG"""
        return self.parseC()

    def parseC(self):
        from lxml import etree
        from lxml.html import fromstring, tostring
        portal_transforms=getToolByName(self.context, 'portal_transforms')
        text = self.context.cuerpo.raw
        data = portal_transforms.convertTo('text/html', text, mimetype='text/-x-web-intelligent')
        html = data.getData()
        parseado=etree.HTML(html)
        flag=0

        flag1=-1
        flag2=-1

        imgs=[]

        for element in parseado.iter("*"):
            if element.tag=="img":
                titulo=""
                if "title" in element.keys():
                    titulo=element.attrib["title"]
                imgs.append({"src":element.attrib["src"],"titulo":titulo})

        return imgs